package com.tn.po.server;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tn.bo.Company;
import com.tn.bo.LastUserLogin;
import com.tn.bo.UserLoginDetails;
import com.tn.bo.UserProfile;
import com.tn.dao.CompanyDAO;
import com.tn.dao.NotificationsDAO;
import com.tn.dao.UserDAO;

@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws IOException, ServletException {
		if ("logout".equalsIgnoreCase(request.getParameter("action"))) {
			request.getSession().invalidate();
			request.getSession(true);
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/index.jsp");
			rd.forward(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		HttpSession session = request.getSession(true);
		String username = request.getParameter("username");
		
		//String password = request.getParameter("password");
		String preference = request.getParameter("viewPreference");
		UserDAO uDao = new UserDAO();
		CompanyDAO cDao = new CompanyDAO();
		UserLoginDetails details = uDao.findUserByUserId(username);
		UserProfile userProfile = uDao.getUserProfile(details
				.getUserProfileId());
		Company company = cDao.findCompanyByCompanyId(userProfile
				.getCompanyId());
		
		NotificationsDAO nDao = new NotificationsDAO();
		LastUserLogin lastLogin = nDao.getLastLogin((userProfile
				.getCompanyId()));
		if(lastLogin != null){
			UserProfile userProfile2 = uDao.getUserProfile(lastLogin.getUserProfileId());
			session.setAttribute("lastLoginTime", lastLogin.getLastLoginTime());
			session.setAttribute("lastLoginUser", userProfile2.getEmailAddress());
		}else{
			session.setAttribute("lastLoginTime", null);
			session.setAttribute("lastLoginUser", null);
		}
		
		nDao.insertLastLogin(userProfile
				.getCompanyId(), userProfile.getUserProfileId());
		
		session.setAttribute("loggedInUser", details.getUserId());
		session.setAttribute("userProfile", userProfile);
		session.setAttribute("userCompany", company);
		session.setAttribute("companyAdmin", "Y");
		session.setAttribute("viewPreference", preference);
		
		
		
		//RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/main.jsp");
		//rd.forward(request, response);
		
		if("buyer".equals(preference)){
			session.setAttribute("viewPreference", "Buyer");
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/phase2/buyerMain.jsp");
			rd.forward(request, response);
		} else{
			session.setAttribute("viewPreference", "Supplier");
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/phase2/supplierMain.jsp");
			
			rd.forward(request, response);
		}
		
		


	}
}
